package sms.impl;

import com.cloopen.rest.sdk.CCPRestSmsSDK;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sms.AbstractSmsApi;
import sms.model.Result;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Created by zijun.song on 2015/9/19.
 */
public class YuntongxunSms extends AbstractSmsApi {


    private static final String SERVER_TEST = "sandboxapp.cloopen.com";
    private static final String SERVER_ONLINE = "app.cloopen.com";
    private static final String SERVER_PORT_TEST = "8883";
    private static final String SERVER_PORT_ONLINE = "8883";

    private static final String ACCOUNT_SID = "8a48b5514f1702fd014f2201f4091008";
    private static final String AUTH_TOKEN = "3f1a1b03dafd4dc1a6d00c9846cad7be";
    private static final String APPID = "8a48b5514fd49643014fe460de60285a";

    private Logger logger = LoggerFactory.getLogger(getClass());

    private static Map<Byte,String> templateIdMap = new HashMap<Byte, String>();

    static{
        templateIdMap.put(MESSAGE_TEMPLATE_USER_BIND_PHONE,"39259");
        templateIdMap.put(MESSAGE_TEMPLATE_USER_REGISTER,"39259");
        templateIdMap.put(MESSAGE_TEMPLATE_USER_LOGIN,"39259");
        templateIdMap.put(MESSAGE_TEMPLATE_USER_REFUND,"39259");
    }


    @Override
    protected String getTemplateByType(Byte id) {
        return templateIdMap.get(id);
    }

    @Override
    public Result sendMsg(String phone, Byte templateId, String[] args) {
        HashMap<String, Object> result = null;

        //初始化SDK
        CCPRestSmsSDK restAPI = new CCPRestSmsSDK();

        //restAPI.init(SERVER_TEST, SERVER_PORT_TEST);
        restAPI.init(SERVER_ONLINE, SERVER_PORT_ONLINE);

        restAPI.setAccount(ACCOUNT_SID, AUTH_TOKEN);

        restAPI.setAppId(APPID);
        result = restAPI.sendTemplateSMS(phone,getTemplateByType(templateId) ,args);
        System.out.println("SDKTestGetSubAccounts result=" + result);
        String statusCode = result.get("statusCode").toString();
        if("000000".equals(statusCode)){
            HashMap<String,Object> data = (HashMap<String, Object>) result.get("data");
            Set<String> keySet = data.keySet();
            for(String key:keySet){
                Object object = data.get(key);
                logger.info(key + " = " + object);
            }
            return Result.returnSuccess(Result.SMS_SEND_SUCCESS);
        }else if ("112300".equals(statusCode) || "112306".equals(statusCode)){
            logger.info("错误码=" + result.get("statusCode") +" 错误信息= "+result.get("statusMsg"));
            return Result.returnFail(Result.INVALID_PHONE_NUMBER);
        }else{
            logger.info("错误码=" + result.get("statusCode") +" 错误信息= "+result.get("statusMsg"));
            return new Result(false,Result.SMS_SEND_FAILURE, result.get("statusMsg").toString());
        }
    }

    @Override
    public Result sendMsg(String[] phones, Byte templateId, String[] args) {
        return  new Result(false, Result.SMS_NOT_SUPPORT_PHONES, "暂不支持多个号码发送");
    }
}